package swings.frames;

import swings.others.CommonJTextField;
import swings.panels.RecordPanel;
import utils.CommonUtils;
import utils.DataManageUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Title: 编辑标题frame
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class EditRecordFrame extends JFrame {

    private Container contentPane = this.getContentPane();
    private JPanel titlePanel = new JPanel();
    private JLabel titleLabel = new JLabel("Title:", JLabel.LEFT);
    private CommonJTextField titleField = new CommonJTextField(20);
    private JButton saveBtn = new JButton("Save");
    private Color bgColor = CommonUtils.bgColor;
    private Color inputColor = CommonUtils.inputColor;
    private Font font = CommonUtils.getFont();
    private RecordPanel recordPanel;
    private int id;
    private String title;

    /**
     * 构造方法
     */
    public EditRecordFrame(RecordPanel recordPanel, int id, String title) {
        this.recordPanel = recordPanel;
        this.id = id;
        this.title = title;
        //初始化组件
        initComponent();
        //初始化按钮
        initBtn();
        //初始化窗口
        init();
    }

    /**
     * 初始化组件
     */
    private void initComponent(){
        contentPane.setLayout(null);
        titlePanel.setBounds(0,5,390,40);
        titleLabel.setFont(font);
        titleField.setFont(font);
        titleLabel.setForeground(Color.WHITE);
        titlePanel.setBackground(bgColor);
        titleField.setBackground(inputColor);
        titleField.setForeground(Color.WHITE);
        titleField.setText(title);
        titlePanel.add(titleLabel);
        titlePanel.add(titleField);
        saveBtn.setBounds(160,50,70,30);
        saveBtn.setFont(font);
        contentPane.add(titlePanel);
        contentPane.add(saveBtn);
        contentPane.setBackground(bgColor);
    }

    /**
     * 初始化按钮
     */
    private void initBtn(){

        //保存按钮监听器
        saveBtn.addActionListener(e -> {
            String title = titleField.getText();
            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put("id",id);
            valueMap.put("title",title);
            try {
                //修改数据
                Connection connectionNet = DataManageUtils.getConnection();
                Statement statementNet = connectionNet.createStatement();
                DataManageUtils.updateRecordTitle(statementNet, valueMap);
                statementNet.close();
                connectionNet.close();
                recordPanel.initRecords("");
                recordPanel.recordMap.get(id).choose();
            } catch (ClassNotFoundException | SQLException | IOException ex) {
                ex.printStackTrace();
            }
            cleanFrame();
            dispose();
        });

    }

    /**
     * 初始化窗口
     */
    public void init(){
        this.setTitle("EditTitle");
        this.setIconImage(ImageUtils.logo);
        this.setLocation((CommonUtils.screenWidth-400)/2, (CommonUtils.screenHeight-220)/2);
        this.setSize(400,130);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * 清空jframe
     */
    private void cleanFrame(){
        titleField.setText("");
    }

}
